package main

import (
	"github.com/ziutek/mymysql/mysql"
	_ "github.com/ziutek/mymysql/thrsafe"
	"log"
)

func Db_select(get_user string, get_password string) (re_val int64, des string) {

	db := mysql.New("tcp", "", "192.168.1.40:3306", "ulsa", "123", "myblog")

	if err := db.Connect(); err != nil {
		log.Println("数据库连接失败。。", err)
		return 3, "数据库连接失败。。"
	}
	defer db.Close()

	rows, res, err := db.Query("select * from user where account = '%s'", get_user)
	if err != nil {
		log.Println(err)
		log.Println("查询数据库失败")
		return 2, "查询数据库失败"
	}

	if len(rows) <= 0 {
		return 5, "未找到此人.."
	}

	//--------------------
	name := res.Map("password")
	user_password := rows[0].Str(name)

	if user_password != get_password {
		log.Println("密码输入错误")
		return 6, "密码输入错误"
	} else if user_password == rows[0].Str(name) {
		return 7, "密码正确"
	} else {
		return 4, "未知错误"
	}
}
func Db_insert(name string, user string, pass string, sex string, age string, address string, email string, phone string) (re_val3 int) {
	db := mysql.New("tcp", "", "192.168.1.40:3306", "ulsa", "123", "myblog")

	if err := db.Connect(); err != nil {
		log.Println("数据库连接失败。。", err)
		return 3
	}
	defer db.Close()
	re_vala, _ := Db_select(user, pass)
	if re_vala == 5 {
		stmt, err := db.Prepare("insert into user(`name`,`account`,`password`,`sex`,`age`,`email`,`address`,`ext`) values(?,?,?,?,?,?,?,?)")

		if err != nil {
			log.Println(err)
			log.Println("插入数据库失败")
			return 4
		}
		// sql参数
		result, hh, err := stmt.Exec(name, user, pass, sex, age, email, address, phone)
		if err != nil {
			log.Println(err, result, hh)
			return 4
		}
		return 2
	} else {
		return 6
	}

}
